#include <BulletClassDataSource/CollisionObjectBullet.h>
#include <osg/Node>
#include <iostream>
#include <EDesignerCore.h>
#include <QDateTime>
#include <osgbCollision/Utils.h>
#include <EngineFunctions.h>
#include <PhysicsWorldFunctions.h>
#include <SceneViewerFunctions.h>
CollisionObjectBullet::CollisionObjectBullet( QObject *parent /*= 0*/ ) : ObjectBullet(parent)
{
    m_collisioinObj = new btCollisionObject();    
    refreshItemTest();
    refreshInfoContainer();
    addRecoverPropertyName("WorldTransform");
}

CollisionObjectBullet::~CollisionObjectBullet()
{
    delete m_collisioinObj;
}

void CollisionObjectBullet::refreshInfoContainer()
{
    m_info->setObjectInspectorItem(this->getObjItem());
    m_collisioinObj->setUserPointer(this->getInfoContainer());

}

QMatrix4x4 CollisionObjectBullet::getWorldTransform()
{
    QMatrix4x4 qMatrix;

    if(m_collisioinObj != NULL)
    {
        osg::Matrix osgMatrix = osgbCollision::asOsgMatrix( m_collisioinObj->getWorldTransform());

        osg::Vec3d trans = osgMatrix.getTrans();
        qMatrix(0,3) = trans.x();
        qMatrix(1,3) = trans.y();
        qMatrix(2,3) = trans.z();

        osg::Vec3d rotate = SceneViewerFunctions::osg_matrix_getRotate(osgMatrix);
        qMatrix(0,2) = rotate.x();
        qMatrix(1,2) = rotate.y();
        qMatrix(2,2) = rotate.z();

    }
    return qMatrix;
}

void CollisionObjectBullet::setWorldTransform(const QMatrix4x4 matrix)
{
    if(m_collisioinObj != NULL)
    {
        osg::Matrix osgMatrix = SceneViewerFunctions::osg_matrix_RotateTrans(osg::Vec3(matrix(0,2),matrix(1,2), matrix(2,2)),osg::Vec3(matrix(0,3),matrix(1,3), matrix(2,3)));
        m_collisioinObj->setWorldTransform(osgbCollision::asBtTransform(osgMatrix));
    }
}

int CollisionObjectBullet::getCollisionflags()
{
    return m_collisioinObj->getCollisionFlags();
}

void CollisionObjectBullet::setCollisionflags(int flags)
{
    m_collisioinObj->setCollisionFlags(flags);
}

void CollisionObjectBullet::setCollisionObj(btCollisionObject *collisioinObj)
{
    delete m_collisioinObj;
    m_collisioinObj = collisioinObj;
}
